//@ sourceURL=changeExamine.js 
CommonUtils.regNamespace("com.im.changeExamine");
com.im.changeExamine.invoke = function() {
	var param = {
		pageNumber: 1,
	    pageSize: 20
	};
    var client = new com.im.common.ajax();
    var operType = "";
    var changeExamine;
    function _init() {
    	//重置按钮
    	$('#ResetBtn').click(function (){
    		_resetSearchInfo();
    	});
        //查询按钮
        $("#SearchBtn").click(function(){
        	_loadDataList(true);
        });
        //审核保存按钮
        $("#saveBtn").click(function(){
        	_save();
        });
        //复选框
        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
          checkboxClass: 'icheckbox_minimal-blue',
          radioClass   : 'iradio_minimal-blue'
        });

        //所属规划-建设单位-实施项目
        $("#searchOrgName").project2unit2itemCommpent({
            elmIdPlanTypeSel : "searchPlanProjClass",
            elmIdUnitSel : "searchOrgName",//组织单位下拉元素
            elmIdItemSel : "searchImpleProjName",//实施项目下拉元素
            orgType : "10",//初始化单位时，单位类型
            callbackFunc: function (callBackType, ret) {//下拉框值改变时回调
                _loadDataList(true);
            }
        });

        //下拉框
        $('.select2').select2();

        //加载列表信息
        _initDataList();
    };
    //重置搜索条件
    function _resetSearchInfo(){
        $('#searchOrgName').val("").trigger("change");
        $('#searchPlanProjClass').val("").trigger("change");
        $('#searchImpleProjName').val("").trigger("change");
    	$('#searchContractCode,#searchContractName').val('');
    }
    //更新数据列表
    function _loadDataList(pageNumber){
        var impleProjName = '';
    	if($("#searchImpleProjName option:selected").attr("value") != '') {
            impleProjName = $("#searchImpleProjName option:selected").text();
		}
    	var contractCode = $("#searchContractCode").val();
    	var contractName = $("#searchContractName").val();
        var planProjClass = $("#searchPlanProjClass").val();
        var developOrg = $("#searchOrgName").val();
        param.impleProjName = impleProjName;
        param.developOrg = developOrg;
        param.planProjClass = planProjClass;
        if(contractCode == ''){
        	delete param.contractCode;
        }else{
        	param.contractCode = contractCode;
        }
        param.contractName = contractName;
        if(pageNumber){
    		param.pageNumber = 1;
            $('#changeExamineList').datagrid("load",param);
        }else{
    	   $('#changeExamineList').datagrid("reload",param);
        }
    }
    //审核
    function _save(){
	    var saveparam={};
    	saveparam.contItemChangeId= $("#contItemChangeId").val();
    	saveparam.contractClauseId= $("#contractClauseId").val();
    	saveparam.conItemId= $("#conItemId").val();
	    saveparam.type= $("#examineResult option:selected").attr("value");
    	saveparam.description= $("#examineDescription").val();
	    var result = client.callAsJson("contractService/examineContractItemChange", saveparam);
	    if(result.resultCode=='0'){
	    	$("#addEditModal").modal("hide");
	        toastr.success("保存成功！");
	    	_loadDataList();
	    }else {
	        toastr.error("服务器忙！请稍后再试！"+result.resultMsg);
	    }
    }
    //审核操作
    function _examineList(index){
    	changeExamine = $('#changeExamineList').datagrid('getData').rows[index];
        $('#contItemChangeId').val(changeExamine.contItemChangeId);
        $('#contractClauseId').val(changeExamine.contractClauseId);
        $('#conItemId').val(changeExamine.conItemId);
        $("#addEditModal").modal("show");
    }
  //合并单元格
    function mergeCellsByField(tableID, fldList,other) {
    	var Arr = fldList.split(",");  
        var dg = $('#' + tableID);  
        var fldName;  
        var RowCount = dg.datagrid("getRows").length;  
        var span;  
        var PerValue = "";  
        var CurValue = "";  
        var length = Arr.length - 1;  
        var otherNum = 1;
        for (i = length; i >= 0; i--) {  
            fldName = Arr[i];  
            PerValue = "";  
            span = 1;  
            for (row = 0; row <= RowCount; row++) {  
                if (row == RowCount) {  
                    CurValue = "";  
                }  
                else {  
                    CurValue = dg.datagrid("getRows")[row][fldName];  
                }  
                if (PerValue === CurValue) {  
                    span += 1;  
                }  
                else {  
                    var index = row - span;  
                    dg.datagrid('mergeCells', {  
                        index: index,  
                        field: fldName,  
                        rowspan: span,  
                        colspan: null  
                    });  
                    if(fldName == 'contractCode'){
                        var name = other.split(","); 
                        $.each(name,function (i,item){
                        	dg.datagrid('mergeCells', {  
                                index: index,  
                                field: item,  
                                rowspan: span,  
                                colspan: null  
                            });
                        });
                    }
                    span = 1;  
                    PerValue = CurValue;  
                }  
            }  
            otherNum++;
        }  
    }
    //初始化表格数据
    function _initDataList(){
    	var columns=[[
    	    {field:'index',title:"序号",width:40,resizable:true,
    	              	formatter: function(value,row,index){
    	                      return  index+1;
    	                  }
    	    },
    	    {field:'impleProjName',title:"实施项目名称",width:120,resizable:true,align:'center'},
    	    {field:'contractCode',title:"合同编号",width:130,resizable:true,align:'center'},
    	    {field:'contractName',title:"合同名称",width:100,resizable:true,align:'center'},
    	    {field:'contractType',title:"合同类型",width:80,resizable:true,align:'center'},
    	    {field:'contractTotal',title:"总金额<br/>（万元）",resizable:true,align:'center'},
    	    //{field:'contractClauseName',title:"支付条款",width:80,resizable:true,align:'center'},
            {field:'contractClauseName',title:"支付条款",width:290,fitColumns:true,resizable:true,align:'left',
                formatter: function(value,row,index){
                    if(value){
                        if(value.length > 120){
                            return "<span style='cursor:pointer'><a onclick='com.im.changeExamine.invoke.showSubjectContent(\""+row.contractName+"\",\""+ row.contractClauseId +"\")'>" + value.substr(0,65) + "...</a></span>";
                        }else{
                           return "<span style='cursor:pointer'><a onclick='com.im.changeExamine.invoke.showSubjectContent(\""+row.contractName+"\",\""+ row.contractClauseId +"\")'>" + value + "</a></span>";
                        }
                    }
                }
            },

    	    {field:'contractItemName',title:"分项名称",width:100,resizable:true,align:'center'},
    	    {field:'amount',title:"数量<br/>（万元）",resizable:true,align:'center'},
    	    {field:'unitPrice',title:"单价<br/>（万元）",resizable:true,align:'center'},
    	    {field:'otherCharge',title:"其它费用<br/>（万元）",resizable:true,align:'center'},
    	    {field:'itemTotal',title:"分项总额",width:60,resizable:true,align:'center'},
    	    {field:'statusCd',title:"状态",width:80,resizable:true,align:'center'},
        	{field:'description',title:"备注",width:160,resizable:true,align:'center'},
    	    {field:'operation',title:"操作",width:40,align:'center',
    	        formatter: function(value,row,index){
    	            return  '<a href="javascript:; "onclick="com.im.changeExamine.invoke.examineList(\''+index+'\')" title="审核"><i class="fa fa-pencil"></i></a> ';
    	    }},
    	]];
    	$('#changeExamineList').datagrid({
    	    url:'/InfoManageWeb/contractService/queryChangeExamineList',
    	    method:'post',
    	    dataType : 'json',
    	    queryParams:param,
    	    pagination:true,
    	    nowrap:false,
    	    fitColumns:true,
    	    columns:columns,
    	    loadMsg:'数据加载中，请耐心等待',
    	    onBeforeLoad : function(param){
	    	    for (var key in param) {
		    	    if(key=="rows"){
		    	    	delete param[key];
		    	    }
		    	}
    	    },
    	    loadFilter:function(data){
    	      if(data.resultCode!=0){
    	        toastr.error(data.resultCode);
    	        return null;
    	      }else{
    	        return data.result;
    	      }
    	    },
    	    onLoadSuccess: function (data) {
                if (data.rows.length > 0) {
                	mergeCellsByField('changeExamineList', "impleProjName,contractCode,contractName,contractType",'operation,contractTotal,contractClauseName');
                }
            }
    	});
    	var p = $('#changeExamineList').datagrid('getPager');
    	  $(p).pagination({
    	    pageSize: 20,
    	    pageList: [10,20,30],
    	    beforePageText: '第',
    	    afterPageText: '页    共 {pages}  页',
    	    displayMsg: '当前显示 {from} - {to} 条记录 共 {total}  条记录',
    	    onSelectPage:function(pageNumber, pageSize){
    	      param.pageNumber=pageNumber;
    	      param.pageSize=pageSize;
    	      	var impleProjName = $("#searchImpleProjName").val();
    	    	var contractCode = $("#searchContractCode").val();
    	    	var contractName = $("#searchContractName").val();
    	        param.impleProjName = impleProjName;
    	        if(contractCode == ''){
    	        	delete param.contractCode;
    	        }else{
    	        	param.contractCode = contractCode;
    	        }
    	        param.contractName = contractName;
    	      $('#changeExamineList').datagrid('reload',param);
    	    }
    	  });
    }

    /**
     * 支付条款
     * @param object
     * @private
     */
    function _showSubjectContent(contractName,contractClauseId) {
        var param= {};
        param.contractClauseId = contractClauseId;
        var resp = client.callAsJson("contractService/qryContractClauseDetail", param);
        if (!resp.success) {
            toastr.error(result.resultMsg);
            return;
        }

        $("#vContractClauseDetailName").text("【"+contractName+"】支付条款");
        $("#vContractClauseDetailNameContent").html(resp.result.contractClauseName);
        $("#contractClauseDetail").modal("show");
    }

	return {
		init : _init,
		examineList : _examineList,
	    showSubjectContent:_showSubjectContent
	}
}();

$(function() {
	com.im.changeExamine.invoke.init();
});
